package org.hcf.api.tencent.callback;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.hcf.api.base.Const;
import org.hcf.api.base.Helper;
import org.hcf.api.tencent.callback.info.group.GroupCreateInfo;
import org.hcf.api.tencent.callback.info.group.base.MemberInfo;
import org.hcf.api.intf.ICallBack;
import org.hcf.common.util.TimeUtils;
import org.hcf.data.RedisUtils;
import org.hcf.data.SqlHelper;
import org.hcf.data.entity.ChatGroup;
import org.hcf.data.entity.ChatGroupInfo;
import org.hcf.data.service.IChatGroupInfoService;
import org.hcf.data.service.IChatGroupService;

import java.util.Set;

@Slf4j
/** 群创建回调 */
public class CallbackGroupCreateHandler implements ICallBack {
    @Override
    public String getCommand() {
        return Const.CMD.GROUP_CREATE_AFTER; //"Group.CallbackAfterCreateGroup";
    }

    int staIndex = 0;


    @Override
    public JSONObject action(JSONObject data) {
        log.info("收到创建群组之后回调:" + data);
        GroupCreateInfo info = data.toJavaObject(GroupCreateInfo.class);
        log.info("反序列化建群信息:" + JSONObject.toJSON(info));
//        JSONObject data = JSONObject.parseObject(body);
        if (!getCommand().equals(info.getCallbackCommand())) {
            log.error("建群消息中[" + data.getString("CallbackCommand") + "]命令不匹配");
            return Helper.toSuccess();
        }
        ChatGroup group = createGroup(info);
        Set<Long> uids = Helper.getPluginUserIds();
        String account = info.getOperator_Account();
        if (NumberUtil.isNumber(account) && uids.contains(Long.parseLong(account))) {
            long userId = Long.parseLong(account);
            ChatGroupInfo memberInfo = addGroupMember(userId, info.getGroupId(), group);
            if (memberInfo != null) {
                RedisUtils.initGA2UID(info.getGroupId(), userId);
                RedisUtils.initUID2GMID(userId, memberInfo.getInfoId());
            }
        }
        for (MemberInfo member : info.getMemberList()) {
            if (member != null && NumberUtil.isNumber(member.getMember_Account())) {
                long userId = Long.parseLong(member.getMember_Account());
                ChatGroupInfo memberInfo = addGroupMember(userId, info.getGroupId(), group);
                if (uids.contains(userId)) {
                    RedisUtils.initGA2UID(info.getGroupId(), userId);
                    if (memberInfo != null) {
                        RedisUtils.initUID2GMID(userId, memberInfo.getInfoId());
                    }
                }
            }
        }
        return Helper.toSuccess();
    }

    ChatGroup createGroup(GroupCreateInfo info) {
        ChatGroup group = new ChatGroup();
        group.setGroupAccount(info.getGroupId());
        group.setMaster(Long.parseLong(info.getOwner_Account()));
        group.setModifyTime(TimeUtils.now());
        group.setCreateTime(group.getModifyTime());
        group.setGroupName(info.getName());
        SqlHelper.getService(IChatGroupService.class).save(group);
        RedisUtils.putGroup(group);
        return group;
    }

    public static ChatGroupInfo addGroupMember(long userID, String groupAccount, ChatGroup group) {
        if (group == null) {
            group = RedisUtils.getGroupByAccount(groupAccount);
        }
        if (group == null) {
            log.error("****************** 群[" + groupAccount + "]不存在 ******************");
            return null;
        }
        ChatGroupInfo info = RedisUtils.getGroupMember(userID, group.getGroupId());
        boolean isin = true;
        if (info == null) {
            isin = false;
            info = new ChatGroupInfo();
        }
        if (userID == group.getMaster()) info.setGroupRole(Const.GroupRole.Master.NUM);
        info.setUserId(userID);
        info.setGroupId(group.getGroupId());
        info.setIsTop(0L);
        info.setKeepGroup(true);
        if (!isin) info.setCreateTime(TimeUtils.now());
        if (!isin) info.setModifyTime(info.getCreateTime());
        if (isin) SqlHelper.getService(IChatGroupInfoService.class).updateById(info);
        else SqlHelper.getService(IChatGroupInfoService.class).save(info);
        return info;
    }
}
